package www.rlicker.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.FeedbackClass;
import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IFeedbackClassService;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.MessageConstant;

import com.alibaba.fastjson.JSONArray;

@Controller  
@RequestMapping("/feedbackclass")
public class FeedbackClassController extends BaseController{
	
	@Autowired
	private IFeedbackClassService service;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	/**
	 * 跳转建议分类页面
	 * @return
	 */
	@RequestMapping("/showlist")
	public String showlist(ModelMap map, HttpServletRequest request){
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
		return "feedback/feedbackClassList";
	}

	/**
	 * 获取建议分类
	 * @param request
	 * @return
	 */
	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request){
		 Criteria<FeedbackClass> param = new Criteria<FeedbackClass>();
	     //param.setStartIndex(Integer.parseInt(request.getParameter("pageNumber")));
	     List<Map<String,Object>> list = service.queryPage(param);
	     PageResult pageResult = new PageResult();
		 pageResult.setRows(list);
		 return pageResult;	
	}
	
	/**
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping("/detail")
	public String detail(String id,ModelMap map){
		FeedbackClass bkClass = service.queryEntity(new Long(id));
		map.put("bkClass", bkClass);
		return "feedback/feedbackClassDetail";
	}
	
	/**
	 * 跟根据id删除建议类别
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public JsonResult delete(String id) {
		JsonResult jr = JsonResult.newInstance();
		String ret = service.deleteAll(id);
	    String content = "删除了"+id+"建议类别";
	    insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}

	/**
	 * 添加建议类别
	 * 
	 * @param id
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = { RequestMethod.POST })
	public JsonResult insert(FeedbackClass bClass){
		JsonResult jr = JsonResult.newInstance();
		FeedbackClass byName = service.queryByName(bClass.getSuggestName());
		if (byName != null) {
			return  jr.declareFailure(MessageConstant.USER_TYPE_1);
		}
		String ret = service.insert(bClass);
		String content = "添加了"+bClass.getSuggestName()+"建议类别";
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}

	/**
	 *跟根据id修改建议类别
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public JsonResult update(FeedbackClass bClass) {
		JsonResult jr = JsonResult.newInstance();
		FeedbackClass byName = service.queryByName(bClass.getSuggestName());
		if (byName != null) {
			return  jr.declareFailure(MessageConstant.USER_TYPE_1);
		}
		Criteria<FeedbackClass> param = new Criteria<FeedbackClass>();
		param.setRecord(bClass);
		param.addParam("id", bClass.getId());
		String ret = service.update(param);
		String content = "修改了"+bClass.getSuggestName()+"建议类别";
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}
}
